package com.example.pagergallery

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.google.gson.Gson

class GalleryViewModel(application: Application) : AndroidViewModel(application) {
    private val _photoListLive = MutableLiveData<List<PhotoItem>>()
    val photoListLive : LiveData<List<PhotoItem>>
    get() = _photoListLive

    fun fetchData(){
        val stringRequest = StringRequest(
            Request.Method.GET,
            getUrl(),
            Response.Listener {
                _photoListLive.value = Gson().fromJson(it,Pixabay::class.java).hits.toList()
            },
            Response.ErrorListener {
                Log.d("hello", it.toString())
            }

        )
        VolleySingleton.getInstance(getApplication()).requestQueue.add(stringRequest)
    }

    private fun getUrl():String {
        return "https://pixabay.com/api/?key=28897448-3a1e9261edea4628f60ead645&q=${keyWords.random()}&per_page=100"
    }

    private val keyWords = arrayOf("cat","dog","car","beauty","photo","computer","flower","animal","backgrounds", "fashion",
        "nature", "science", "education", "feelings", "health", "people", "religion", "places", "animals", "industry",
        "computer", "food", "sports", "transportation", "travel", "buildings", "business", "music","park","school","room",
        "sea","women","book","students","sky","landscape","technology","beauty","city","background","mid-autumn",
        "starry","architecture","forest","happy","birthday", "technology", "landscape", "starry sky","anime",
        "people", "moon", "illustration", "character", "cartoon","love","cute","belle","yellow","red","green","blue","pink",
        "gold","white","black","smoke","bridge","panda","bird","butterfly","forest","sun")


}